package com.example.ymm.modules1.controller.common;

import com.alibaba.fastjson.JSONObject;
import com.example.ymm.model.CommonResult;
import com.example.ymm.modules1.controller.BaseController;
import com.example.ymm.modules1.entity.hl.ex.ExTeamUser;
import com.example.ymm.modules1.entity.my.sys.SysNoteMsg;
import com.example.ymm.modules1.entity.my.user.DbUser;
import com.example.ymm.modules1.query.sys.SysNoteMsgQueryCriteria;
import com.example.ymm.modules1.service.dd.ms.JsapiH5Service;
import com.example.ymm.modules1.service.hl.YwApproveService;
import com.example.ymm.modules1.service.hl.ex.ExTeamUserService;
import com.example.ymm.modules1.service.hl.ex.ExUserAnserService;
import com.example.ymm.modules1.service.ms.MsOrderItemViewService;
import com.example.ymm.modules1.service.sys.SysNoteMsgService;
import com.example.ymm.modules1.service.sys.SysSettingService;
import com.example.ymm.modules1.service.user.*;
import com.example.ymm.util.UtilValidate;

import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/hl/common")
@Tag(name =  "系统信息管理")
@Slf4j
@CrossOrigin
public class CommonController extends BaseController {

    @Autowired
    ExTeamUserService exTeamUserService;

    @Autowired
    SysSettingService sysSettingService;

    @Autowired
    DbApprovalLogService dbApprovalLogService;

    @Autowired
    DbCardService dbCardService;

    @Value("${spring.profiles.active}")
    private String active;
    @Autowired
    JsapiH5Service jsapiH5Service;

    @Autowired
    SysNoteMsgService sysNoteMsgService;

    @Autowired
    DbChatGroupDeptService dbChatGroupDeptService;
    @Autowired
    YwApproveService ywApproveService;

    @Autowired
    DbUserService dbUserService;

    @Autowired
    ExUserAnserService exUserAnserService;
    @Autowired
    DbChatGroupService dbChatGroupService;
    @Autowired
    MsOrderItemViewService msOrderItemViewService;

    @Schema( name =  "获取职级下拉列表")
    @RequestMapping(value = "/getLevelSelects", method = RequestMethod.POST)
    public CommonResult getLevelSelects() {
        try{

            String s="A1,A2,A3,B1,B2,C1,C2";
            String[] split = s.split(",");
            List<JSONObject> list=new ArrayList<>();
            for(String s1:split){
                JSONObject json=new JSONObject();
                json.put("label",s1);
                json.put("value",s1);
                list.add(json);
            }
            return CommonResult.success(list);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Schema( name =  "获取书本分类下拉列表")
    @RequestMapping(value = "/getCategorySelects", method = RequestMethod.POST)
    public CommonResult getCategorySelects() {
        try{

            String s="必读,拓展";
            String[] split = s.split(",");
            List<JSONObject> list=new ArrayList<>();
            for(String s1:split){
                JSONObject json=new JSONObject();
                json.put("label",s1);
                json.put("value",s1);
                list.add(json);
            }
            return CommonResult.success(list);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Schema( name =  "排行榜")
    @RequestMapping(value = "/getRankings", method = RequestMethod.POST)
    public CommonResult getRankings(Integer totalType,String date_str) {
        try{

            /**
             * 统计方式 1 从起始日期开始统计 2 当前月份 3 按当年统计
             */
            List<DbUser> list = dbCardService.getRankings(totalType,date_str);
            return CommonResult.success(list);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Schema( name =  "获得jsapi鉴权")
    @RequestMapping(value = "/getConfig", method = RequestMethod.POST)
    public CommonResult getConfig(String url) {
        try{

            JSONObject config = jsapiH5Service.getConfig(url);

            return CommonResult.success(config);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @PostMapping("/getSysNoteList")
    @Schema( name =  "首页 轮播图消息展示")
    public CommonResult getSysNoteList(){
        try {
            SysNoteMsgQueryCriteria criteria=new SysNoteMsgQueryCriteria();
            criteria.setStatus(1);
            List<SysNoteMsg> sysNoteMsgs = sysNoteMsgService.queryAll(criteria);
            return CommonResult.success(sysNoteMsgs);
        }catch (Exception e){
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }

    }

    @PostMapping("/approveEndEvent")
    @Schema( name =  "薪福通审批结束事件")
    public JSONObject approveEndEvent(@RequestBody JSONObject jsonObject){
        JSONObject result=new JSONObject();
        try {

           if("XFTOAFPS".equals(jsonObject.getString("eventId"))){
               System.out.println("---EndEvent：");
               System.out.println(jsonObject);
               ywApproveService.approveEndEvent(jsonObject);
            }
            result.put("rtnCod",200);
            result.put("errMsg","");
            return result;
        }catch (Exception e){
            log.error(e.getMessage(), e);
            result.put("errMsg",e.getMessage());
            return result;
        }

    }


    @GetMapping("/syncNoGroupUserDept")
    @Schema( name =  "同步未入群人员")
    public CommonResult syncNoGroupUserDept(){
        try {
            dbChatGroupDeptService.syncNoGroupUserDept();
            return CommonResult.success();
        }catch (Exception e){
           // log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }

    }



    @PostMapping("/refresh")
    @Schema( name =  "刷新token")
    public CommonResult refresh(@RequestParam String userId){
        try {

            return dbUserService.loginByUserId(userId);
        }catch (Exception e){
            // log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }

    }


    @GetMapping("/testUp1")
    @Schema( name =  "testUp1")
    public CommonResult testUp1(@RequestParam String pwd){
        try {
            if(!"123".equals(pwd)){
                return CommonResult.error("无访问权限!");
            }
            dbApprovalLogService.testChangeUser1();
            return CommonResult.success();
        }catch (Exception e){
            // log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }

    }

    @GetMapping("/testUp2")
    @Schema( name =  "testUp2")
    public CommonResult testUp2(@RequestParam String pwd){
        try {
            if(!"123".equals(pwd)){
                return CommonResult.error("无访问权限!");
            }
            dbApprovalLogService.testChangeUser2();
            return CommonResult.success();
        }catch (Exception e){
            // log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }

    }


    @Schema( name =  "测试排序")
    @PostMapping("/testSort")
    public CommonResult testSort() {
        try{
            exTeamUserService.testSort();
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Schema( name =  "获得环境变量")
    @PostMapping("/getActive")
    public CommonResult getActive() {
        try{

            return CommonResult.success(active);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

//    @Schema( name =  "发送机器人群聊")
//    @PostMapping("/sendGroupListMessage")
//    public CommonResult sendGroupListMessage() {
//        try{
//            dbChatGroupService.sendGroupListMessage();
//            return CommonResult.success();
//        }catch (Exception e){
//            log.error(e.getMessage(),e);
//            return CommonResult.error(e.getMessage());
//        }
//    }


    @Schema( name =  "修改题目")
    @PostMapping("/changeQuestionFn")
    public CommonResult changeQuestionFn() {
        try{
            exUserAnserService.changeQuestionFn();
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Schema( name =  "模拟用户排行")
    @PostMapping("/getUsers")
    public CommonResult getUsers() {
        try{
            List<ExTeamUser> exTeamUsers = exTeamUserService.queryAll(null);
            List<ExTeamUser> users=new ArrayList<>();
            for (int i = 0; i < exTeamUsers.size(); i++) {
                ExTeamUser teamUser = exTeamUsers.get(i);
                teamUser.setNum(i+1);
            }
            int tager_index=0;
            int temp_index=0;
            ExTeamUser current=null;
            for (int i = 0; i < exTeamUsers.size(); i++) {
                ExTeamUser teamUser = exTeamUsers.get(i);
//                if(!teamUser.getUserName().equals("刘庆峰")){
//                    users.add(teamUser);
//                }
                if(teamUser.getUserName().equals("刘庆峰")){
                    teamUser.setCurrentUser(true);
                    current=teamUser;
                    tager_index=i;
                }
                users.add(teamUser);
            }
            if(exTeamUsers.size()-1-tager_index>0){
                temp_index=exTeamUsers.size()-1-tager_index;
            }else{
                temp_index=exTeamUsers.size()-1;
            }
            Integer userNum=null;
            if(UtilValidate.isNotEmpty(current)){
                userNum=current.getSort();
            }
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("users",users);
            jsonObject.put("userNum",userNum);
            jsonObject.put("tager_index",tager_index);
            jsonObject.put("temp_index",temp_index);
            return CommonResult.success(jsonObject);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Schema( name =  "生成时俱佳token")
    @PostMapping("/createLoginToken")
    public CommonResult createLoginToken() {
        try{
            String loginToken = msOrderItemViewService.createLoginToken();
            return CommonResult.success(loginToken);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Schema( name =  "重新推送配送费到鑫福通")
    @PostMapping("/testApprovaAllPassFn")
    public CommonResult testApprovaAllPassFn() {
        try{
            dbApprovalLogService.testApprovaAllPassFn();
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


}
